const ws = require('nodejs-websocket')
const moment = require('moment')

function broadcast(obj) {
    server.connections.forEach(function (conn) { // 注意：这里是server,不是ws
        conn.sendText(JSON.stringify(obj)) // 注意：这里得转成字符串发送过去，不然会报错。
    })
}

const server = ws.createServer(function (conn) {
    conn.on('text', function (data) {
        const obj = JSON.parse(data)
        switch (obj.type) {
            case 1:
                broadcast({
                    type: 1,
                    phone: obj.phone,
                    uid: obj.uid,
                    msg: `${obj.phone ? (obj.phone==18896556035 || obj.phone==13279659259 ? '客服小海豹进入了聊天室' : '海豹'+obj.phone.replace(/(\d{7})(\d{4})/, "$2")+'进入了聊天室') : ''}`,
                    date: moment().format('HH:mm:ss')
                })
                break;
            case 2:
                broadcast({
                    type: 2,
                    phone: obj.phone,
                    uid: obj.uid,
                    msg: obj.msg,
                    date: moment().format('HH:mm:ss')
                })
                break;
        }
    })

    conn.on('close', function (e) {
        // console.log(e, '服务端连接关闭')
    })

    conn.on('error', function (e) {
        // console.log(e, '服务端异常')
    })

}).listen(8000)
console.log('websocket已开启')


// 加载Express模块
const express = require('express')

// 加载MySQL模块
const mysql = require('mysql')

// 创建MySQL连接池    8.136.106.157
const pool = mysql.createPool({
  host: '8.136.106.157', //MySQL服务器地址
  port: 3306, //MySQL服务器端口号
  user: 'root', //数据库用户的用户名
  password: '', //数据库用户密码
  database: 'byd_hb', //数据库名称
  connectionLimit: 20, //最大连接数
  charset: 'utf8', //数据库服务器的编码方式
})

//创建WEB服务器
const serve = express()

//中间件...
//把post传参转为对象
serve.use(
  express.urlencoded({
    extended: true, //内部转对象是否使用第三方模块
  })
)


// 加载CORS模块
const cors = require('cors')

// 使用CORS中间件
serve.use(
  cors({
    // origin: ['http://localhost:8888', 'http://127.0.0.1:8888']
    origin: '*',
  })
)

// 配置multer中间件
const multer = require('multer')
obj = multer.diskStorage({
  destination: function (req, file, cb) {
    //指定目录
    cb(null, 'upload')
  },
  filename: function (req, file, cb) {
    // 指定文件名
    let name = file.originalname
    // name:  abcd.jpg    xxxdfdd.zip
    let ext = name.substr(name.lastIndexOf('.'))
    cb(null, uuid.v4() + ext)
  },
})
const uploadTools = multer({
  storage: obj,
})
const uuid = require('uuid')
// 静态资源托管special目录
serve.use(express.static('upload'))

//添加错误处理中间件，拦截所有的错误
serve.use((err, req, res, next) => {
  //err  接收到的路由传递过来的错误
  console.log(err)
  //响应给前端
  res.send({
    code: 500,
    msg: '服务器端错误',
  })
})



// 1.用户注册接口
serve.post('/register', (req, res, next) => {
  // 获取用户名和密码信息
  let phone = req.body.phone
  let pwd = req.body.pwd
  //以phone为条件进行查找操作，以保证用户的唯一性
  let sql = 'select * from byd_user where phone=?'
  pool.query(sql, [phone], (error, results) => {
    if (error) {
      next(error)
      return
    }
    // console.log(results);
    let length = results.length
    if (length == 0) {
      // 将用户的相关信息插入到数据表
      sql = 'insert byd_user(phone,pwd) values(?,?)'
      pool.query(sql, [phone, pwd], (error, results) => {
        if (error) {
          next(error)
          return
        }
        //成功
        res.send({ message: 'register success', code: 200 })
      })
    } else {
      //失败,用户已存在
      res.send({ message: 'user exists', code: 201 })
    }
  })
})

//2.用户登录接口
serve.get('/login/:phone/:pwd', (req, res, next) => {
  // 获取用户名和密码信息
  // console.log(req.params);
  let phone = req.params.phone
  let pwd = req.params.pwd
  //sql语句
  let sql = 'select * from byd_user where phone=? and pwd=?'
  pool.query(sql, [phone, pwd], (error, results) => {
    if (error) {
      next(error)
      return
    }
    // console.log(results);
    if (results.length == 0) {
      res.send({ message: 'login failed', code: 201 })
    } else {
      res.send({ message: 'login success', code: 200 })
    }
  })
})

// 3.后台查询用户分页显示接口
serve.get('/get_user', (req, res, next) => {
  //获取get传递的参数
  //如果没有传参，设置默认值
  //如果页码为空，设置默认值为1
  if (!req.query.page) {
    req.query.page = 1
  }
  //如果每页数据量为空，设置默认值为10
  if (!req.query.num) {
    req.query.num = 10
  }
  //计算开始查询的值
  let start = (req.query.page - 1) * req.query.num
  //每页数据量转为数字
  let size = parseInt(req.query.num)
  //执行SQL命令，查询数据
  pool.query(
    'select * from byd_user limit ?,?',
    [start, size],
    (error, results) => {
      if (error) {
        next(error)
        return
      }
      // console.log(r)
      res.send({ code: 200, msg: 'ok', data: results })
    }
  )
})

// 4.超级管理员修改管理员 update byd_admin_list
serve.post('/upd_admin', (req, res, next) => {
  // 超级管理员修改管理员列表中的数据
  let id = req.body.admin_id
  let root = req.body.admin_name
  let pwd = req.body.admin_pwd
  let sql =
    'update byd_admin_list set admin_name=?,admin_pwd=? where admin_id=?'
  pool.query(sql, [root, pwd, id], (error, results) => {
    if (error) {
      next(error)
      return
    }
    // console.log(results)
    if (results.affectedRows == 0) {
      // 修改失败返回的值
      res.send({ message: 'update false', code: 201 })
    } else {
      // 修改成功返回的值
      res.send({ message: 'update success', code: 200 })
    }
  })
})

// 5.超级管理员删除管理员 delete	 byd_admin_list
serve.delete('/del_admin/:admin_id', (req, res, next) => {
  // console.log(req.params.admin_id);
  // // 删除超级管理员修改管理员表中数据
  let sql = 'delete from byd_admin_list where admin_id=?'
  // console.log(sql)
  pool.query(sql, req.params.admin_id, (error, results) => {
    if (error) {
      // 如果sql中有错误,交给下一个错误处理中间件
      next(error)
      // 阻止向后执行
      return
    }
    // console.log(results)
    if (results.affectedRows == 0) {
      // 删除失败返回的值
      res.send({ message: 'delete false', code: 201 })
    } else {
      // 删除成功返回的值
      res.send({ message: 'delete success', code: 200 })
    }
  })
})

// 6.导航栏搜索接口
serve.get('/search/find', (req, res, next) => {
  // SQL语句以模糊匹配上商品数据
  let sql = 'select * from byd_largepic where title like ? '
  // 执行SQL语句
  //模板字符串
  pool.query(sql, [`%${req.query.title}%`], (error, results) => {
    if (error) {
      next(error)
      return
    }
    if (results.length == 0) {
      res.send({ message: 'no exists', code: 201 })
    } else {
      res.send({ message: 'search success', code: 200, data: results })
    }
  })
})

// 7.首页轮播图 byd_indexcarousel
serve.get('/swiper', (req, res, next) => {
  // SQL语句查询首页的轮播图
  let sql = 'select * from byd_indexcarousel'
  pool.query(sql, (error, results) => {
    if (error) {
      // 如果sql中有错误,交给下一个错误处理中间件
      next(error)
      // 阻止向后执行
      return
    }
    // console.log(results)
    // 结果是数组,把结果响应给客户端
    res.send({ message: 'search success', code: 200, data: results })
  })
})

// 8.预约试驾接口
serve.post('/order', (req, res, next) => {
  let cid = req.body.cid
  let phone = req.body.phone
  let customer_add = req.body.customer_add
  let store_add = req.body.store_add
  let time = req.body.time
  let name = req.body.name
  let tel = req.body.tel
  let order_status = req.body.order_status
  if (
    !cid ||
    !phone ||
    !customer_add ||
    !store_add ||
    !time ||
    !name ||
    !tel ||
    !order_status
  ) {
    res.send({ message: 'no order', code: 201 })
  } else {
    // SQL语句新增参数
    let sql = 'insert into byd_order_list values(null,?,?,?,?,?,?,?,?)'
    pool.query(
      sql,
      [cid, phone, customer_add, store_add, time, name, tel, order_status],
      (error, results) => {
        if (error) {
          next(error)
          return
        }

        if (results.length == 0) {
          res.send({ message: 'no order', code: 201 })
        } else {
          res.send({ message: 'order', code: 200 })
        }
      }
    )
  }
})

//9.车友圈发帖插入接口
serve.post('/club', (req, res, next) => {
  // console.log(req.body)
  let phone = req.body.phone
  let msg_pic1 = req.body.msg_pic1
  let msg_pic2 = req.body.msg_pic2
  let msg_pic3 = req.body.msg_pic3
  let msg = req.body.msg
  let msg_time = req.body.msg_time
  //sql语句
  let sql =
    'insert into byd_fans_message(phone,msg_pic1,msg_pic2,msg_pic3,msg,msg_time) values(?,?,?,?,?,?)'
  pool.query(
    sql,
    [phone, msg_pic1, msg_pic2, msg_pic3, msg, msg_time],
    (error, results) => {
      if (error) {
        next(error)
        return
      }
      res.send({ message: 'publish success', code: 200 })
    }
  )
})

//10.车友圈帖子列表加载   4
serve.get('/clubs', (req, res, next) => {
  // console.log(req.query)
  //获取get传递的参数
  //如果没有传参，设置默认值
  //如果页码为空，设置默认值为1
  if (!req.query.page) {
    req.query.page = 1
  }
  //计算开始查询的值
  let start = (req.query.page - 1) * 4
  //每页数据量转为数字
  // sql语句
  let sql =
    'select * from byd_fans_message where phone is not null order by mid desc limit ?,4'
  pool.query(sql, [start], (error, results) => {
    if (error) {
      next(error)
      return
    }
    // console.log(results)
    if (results.length == 0) {
      res.send({ message: 'inquire failed', code: 201 })
    } else {
      res.send({ message: 'inquire success', code: 200, data: results })
    }
  })
})



//12.增加车友圈帖子点赞数
serve.post('/support', (req, res, next) => {
  // console.log(req.body);
  let mid = req.body.mid
  let scount = parseInt(req.body.scount)
  console.log(mid,scount);
  //sql语句
  let sql = `update byd_fans_message set scount=${scount + 1} where mid=?`
  pool.query(sql, [mid], (error, results) => {
    if (error) {
      next(error)
      return
    }
    res.send({ message: 'add success', code: 200 })
  })
})

// 13.车友圈帖子评论接口
serve.post('/remark', (req, res, next) => {
  // console.log(req.body)
  let mmid = req.body.mid
  let remark_phone = req.body.remark_phone
  let remark_msg = req.body.remark_msg
  let remark_time = req.body.remark_time
  //sql语句
  let sql =
    'insert into byd_comment(mmid,remark_phone,remark_msg,remark_time) values(?,?,?,?)'
  pool.query(
    sql,
    [mmid, remark_phone, remark_msg, remark_time],
    (error, results) => {
      if (error) {
        next(error)
        return
      }
      res.send({ message: 'publish success', code: 200 })
    }
  )
})

// 14.左侧浮动——选择车型与指导价
serve.get('/car', (req, res, next) => {
  // SQL语句查询指导价&利息
  let sql = 'select model,gprice from byd_guide where cid=?'
  // 执行SQL语句
  pool.query(sql, [req.query.cid], (error, result) => {
    // console.log(req.query.cid)
    if (error) {
      next(error)
      return
    }
    if (result.length == 0) {
      res.send({ message: 'no exists', code: 201 })
    } else {
      res.send({ message: 'ok', code: 200, data: result })
    }
  })
})

// 15.详情页收藏进个人中心
serve.post('/fav', (req, res, next) => {
  let cid = req.body.cid
  let phone = req.body.phone
  let title = req.body.title
  let gprice = req.body.gprice
  let big_pic = req.body.big_pic

  let sql = 'insert byd_fav (cid,phone,title,gprice,big_pic) values (?,?,?,?,?)'
  pool.query(sql, [cid, phone, title, gprice, big_pic], (error, result) => {
    if (error) {
      next(error)
      return
    }
    if (result.length == 0) {
      res.send({ message: 'collect fail', code: 201 })
    } else {
      res.send({ message: 'collect success', code: 200 })
      // console.log(result)
    }
  })
})

// 16.车型对比参数
serve.get('/compare', (req, res, next) => {
  // SQL语句查询车辆编号
  let sql =
    'select size_a,	size_b,	size_c,	size_d,	size_e,	size_f,	power_i,	power_n,	brake_a,	brake_b,	brake_e,	brake_f,	safty_a,	safty_b,	safty_c,	safty_d,	safty_e,	safty_f,	safty_g,	safty_h,	safty_i,	safty_k,	safty_o,	safty_y,	assist_b,	assist_f,	assist_g,	assist_h,	assist_m,	assist_q,	facade_f,	facade_h,	trim_e,	trim_w,	dilink_c,	dilink_i,	dilink_j,	lamplight_e,	lamplight_g,	lamplight_h,	lamplight_k,	glass_f,	air_d,	air_e,	air_h,	new_energy_h from byd_car_params where cid=? '
  // 执行SQL语句
  pool.query(sql, [req.query.cid], (error, result) => {
    // console.log(req.query.cid)
    if (error) {
      next(error)
      return
    }
    if (result.length == 0) {
      res.send({ message: 'no exists', code: 201 })
    } else {
      res.send({ message: 'ok', code: 200, data: result })
    }
  })
})

// 17.车型对比大图
serve.get('/pic', (req, res, next) => {
  // SQL语句查询车辆大图
  let sql = 'select * from byd_largepic where cid=?'
  // 执行SQL语句
  pool.query(sql, [req.query.cid], (error, result) => {
    // console.log(req.query.cid)
    if (error) {
      next(error)
      return
    }
    if (result.length == 0) {
      res.send({ message: 'no exists', code: 201 })
    } else {
      res.send({ message: 'ok', code: 200, data: result })
    }
  })
})

// 18.比亚迪海豹所有车型参数(比亚迪海豹cid=1~4)  4
serve.get('/param', (req, res, next) => {
  if (!req.query.page) {
    req.query.page = 1
  }
  //计算开始查询的值
  let start = (req.query.page - 1) * 4
  // SQL语句查询车辆编号
  let sql = 'select * from byd_car_params limit ?,4'
  // 执行SQL语句
  pool.query(sql, [start], (error, result) => {
    if (error) {
      next(error)
      return
    }
    res.send({ message: 'ok', code: 200, data: result })
  })
})

//19.个人中心收藏
serve.get('/collect', (req, res, next) => {
  // console.log(req.query)
  let phone = req.query.phone
  let start = (req.query.page - 1) * 6
  //sql语句
  let sql = 'select * from byd_fav where phone=? limit ?,6'
  pool.query(sql, [phone, start], (error, results) => {
    if (error) {
      next(error)
      return
    }
    // console.log(results);
    if (results.length == 0) {
      res.send({ message: 'inquire failed', code: 201 })
    } else {
      res.send({ message: 'inquire success', code: 200, data: results })
    }
  })
})

// 20.获取预约看车记录评价
serve.get('/score', (req, res, next) => {
  // SQL语句查询车辆编号
  let sql = 'select * from byd_order_remark where rid=? '
  // 执行SQL语句
  pool.query(sql, [req.query.rid], (error, result) => {
    // console.log(req.query.rid)
    if (error) {
      next(error)
      return
    }
    if (result.length == 0) {
      res.send({ message: 'no exists', code: 201 })
    } else {
      res.send({ message: 'ok', code: 200, data: result })
    }
  })
})

// 21.个人中心预约看车
serve.get('/log', (req, res, next) => {
  let phone = req.query.phone
  let sql = 'select * from byd_order_list where phone=?'
  pool.query(sql, [phone], (error, results) => {
    if (error) {
      next(error)
      return
    }
    if (results.length == 0) {
      res.send({ message: 'query fails', code: 201 })
    } else {
      res.send({ message: 'query is successful', code: 200, data: results })
    }
  })
})

//22.获取自己发布的车友圈记录  3
serve.get('/record', (req, res, next) => {
  // console.log(req.query)
  let phone = req.query.phone
  let page = (req.query.page - 1) * 2
  let sql = 'select * from byd_fans_message where phone=? order by mid desc limit ?,2'
  pool.query(sql, [phone, page], (error, results) => {
    if (error) {
      next(error)
      return
    }
    if (results.length == 0) {
      res.send({ message: 'inquire failed', code: 201 })
    } else {
      res.send({ message: 'inquire success', code: 200, data: results })
    }
  })
})

//23. 我回复的帖子获取           2

serve.get('/reply', (req, res, next) => {
  // console.log(req.query)
  let remark_phone = parseInt(req.query.remark_phone)
  //获取get传递的参数
  //如果没有传参，设置默认值
  //如果页码为空，设置默认值为1
  if (!req.query.page) {
    req.query.page = 1
  }
  //计算开始查询的值
  let start = (req.query.page - 1) * 2
  //执行SQL命令，查询数据
  let sql =
    ' select *,remark_phone from byd_fans_message,byd_comment where mid=mmid and remark_phone=? order by mid desc limit ?,2'
  pool.query(sql, [remark_phone, start], (error, results) => {
    if (error) {
      next(error)
      return
    }
    if (results.length == 0) {
      res.send({ message: 'login failed', code: 201 })
    } else {
      res.send({ message: 'ok', code: 200, data: results })
    }
  })
})

// 24.管理员登录接口
serve.get('/admin/:admin_name/:admin_pwd', (req, res, next) => {
  // 获取用户名和密码信息
  let admin_name = req.params.admin_name
  let admin_pwd = req.params.admin_pwd
  //sql语句
  let sql = 'select * from byd_admin_list where admin_name=? and admin_pwd=?'
  pool.query(sql, [admin_name, admin_pwd], (error, results) => {
    if (error) {
      next(error)
      return
    }
    if (results.length == 0) {
      res.send({ message: 'login failed', code: 201 })
    } else {
      res.send({ message: 'ok', code: 200, data: results[0] })
    }
  })
})

// 25.后台管理系统商品中心(修改)
serve.post('/update_car', (req, res, next) => {
  let title = req.body.title
  let lpic = req.body.lpic
  let gprice = req.body.gprice
  let cid = req.body.cid
  if (!title || !lpic || !gprice || !cid) {
    res.send({ message: 'change failed', code: 201 })
  } else {
    let sql = 'update byd_largepic set title=?,lpic=?,gprice=? where cid=?'
    pool.query(sql, [title, lpic, gprice, cid], (error, results) => {
      if (error) {
        next(error)
        return
      }
      // console.log(results)
      if (results.affectedRows == 0) {
        res.send({ message: 'change failed', code: 201 })
      } else {
        res.send({ message: 'modify successfully', code: 200 })
      }
    })
  }
})

//26.后台查询商品分页显示接口 8
serve.get('/get_cars', (req, res, next) => {
  //获取get传递的参数
  //如果没有传参，设置默认值
  //如果页码为空，设置默认值为1
  if (!req.query.page) {
    req.query.page = 1
  }
  //计算开始查询的值
  let start = (req.query.page - 1) * 8
  //执行SQL命令，查询数据
  pool.query(
    'select * from byd_largepic limit ?,8',
    [start],
    (error, results) => {
      if (error) {
        next(error)
        return
      }
      // console.log(results)
      res.send({ code: 200, msg: 'ok', data: results })
    }
  )
})

// 27.后台管理系统商品中心(删)
serve.delete('/del_car/:cid', (req, res, next) => {
  let cid = req.params.cid
  let sql = 'delete from byd_largepic where cid=?'
  pool.query(sql, [cid], (error, results) => {
    if (error) {
      next(error)
      return
    }
    if (results.affectedRows == 0) {
      res.send({ message: 'fail to delete ', code: 201 })
    } else {
      res.send({ message: 'successfully delete', code: 200 })
    }
  })
})

// 28.后台管理系统商品中心(增)  向详情页车辆大图列表插入数据 byd_largepic
serve.post('/post_car', (req, res, next) => {
  // 向详情页车辆大图列表中插入数据
  if (!req.body.title || !req.body.lpic || !req.body.gprice) {
    res.send({ message: 'insert false', code: 201 })
  } else {
    let sql = 'insert byd_largepic(cid,title,lpic,gprice) values(null,?,?,?)'
    pool.query(
      sql,
      [req.body.title, req.body.lpic, req.body.gprice],
      (error, results) => {
        if (error) {
          // 如果sql中有错误,交给下一个错误处理中间件
          next(error)
          // 阻止向后执行
          return
        }
        // console.log(results)
        // 结果是数组,把结果响应给客户端
        res.send({ message: 'insert success', code: 200 })
      }
    )
  }
})

//29. 后台管理系统预约中心(客户端预约试驾)  (查)  10
serve.get('/get_orders', (req, res, next) => {
  //获取get传递的参数
  //如果没有传参，设置默认值
  //如果页码为空，设置默认值为1
  if (!req.query.page) {
    req.query.page = 1
  }
  //如果每页数据量为空，设置默认值为8
  if (!req.query.num) {
    req.query.num = 10
  }
  //计算开始查询的值
  let start = (req.query.page - 1) * req.query.num
  //每页数据量转为数字
  let size = parseInt(req.query.num)
  //执行SQL命令，查询数据
  pool.query(
    'select * from byd_order_list order by oid desc limit ?,?',
    [start, size],
    (error, results) => {
      if (error) {
        next(error)
        return
      }
      // console.log(results)
      res.send({ code: 200, msg: 'ok', data: results })
    }
  )
})

// 30. 后台管理系统预约中心(客户端预约试驾)  (改)
serve.post('/update_order', (req, res, next) => {
  let cid = req.body.cid
  let store_add = req.body.store_add
  let time = req.body.time
  let name = req.body.name
  let tel = req.body.tel
  let order_status = req.body.order_status
  let oid = req.body.oid
  if (!cid || !store_add || !time || !name || !tel || !order_status) {
    res.send({ message: 'change failed', code: 201 })
  } else {
    let sql =
      'update byd_order_list set cid=?,store_add=?,time=?,name=?,tel=?,order_status=? where oid=?'
    pool.query(
      sql,
      [cid, store_add, time, name, tel, order_status, oid],
      (error, results) => {
        if (error) {
          next(error)
          return
        }
        if (results.affectedRows == 0) {
          res.send({ message: 'change failed', code: 201 })
        } else {
          res.send({ message: 'modify successfully', code: 200 })
        }
      }
    )
  }
})

//31.车友圈获取评论  4
serve.get('/getremark', (req, res, next) => {
  let mmid = parseInt(req.query.mid)
  let page = req.query.page
  //获取get传递的参数
  //如果没有传参，设置默认值
  //如果页码为空，设置默认值为1
  if (!page) {
    page = 1
  }
  //计算开始查询的值
  let start = (page - 1) * 4
  //执行SQL命令，查询数据
  pool.query(
    'select * from byd_comment where mmid=? order by pid desc limit ?,4',
    [mmid, start],
    (error, results) => {
      if (error) {
        next(error)
        return
      }
      if (results.length == 0) {
        res.send({ code: 201, msg: 'query failed' })
      } else {
        res.send({ code: 200, msg: 'query success', data: results })
      }
    }
  )
})

// 32.获取大家关注的随机帖子
serve.get('/getclubs', (req, res, next) => {
  //获取帖子总数
  let sql = 'select count(mid) from byd_fans_message'
  pool.query(sql, (error, results) => {
    let count = results[0]['count(mid)']
    // console.log(count);
    // 执行SQL命令，查询数据
    let start = parseInt(Math.random() * count)
    start = Math.min(start, count - 10)
    start = Math.max(start, 0)
    pool.query(
      'select phone,msg from byd_fans_message limit ?,10',
      [start],
      (error, results) => {
        if (error) {
          next(error)
          return
        }
        res.send({ code: 200, msg: 'query success', data: results })
      }
    )
  })
})

//  33.文件上传
// 接收请求
serve.post('/upload', uploadTools.array('uploadFile'), (req, res) => {
  // console.log('req', req.files)
  let urls = []
  req.files.forEach((item) => {
    urls.push(`http://8.136.106.157:8888/${item.filename}`)
  })
  res.send({ code: 200, msg: 'ok', urls })
})

//34.管理员列表
serve.get('/get_admin', (req, res, next) => {
  // console.log(req)
  //获取get传递的参数
  //如果没有传参，设置默认值
  //如果页码为空，设置默认值为1
  if (!req.query.page) {
    req.query.page = 1
  }
  //如果每页数据量为空，设置默认值为8
  if (!req.query.num) {
    req.query.num = 10
  }
  //计算开始查询的值
  let start = (req.query.page - 1) * req.query.num
  //每页数据量转为数字
  let size = parseInt(req.query.num)
  //执行SQL命令，查询数据
  pool.query(
    'select * from byd_admin_list limit ?,?',
    [start, size],
    (error, results) => {
      if (error) {
        next(error)
        return
      }
      // console.log(results)
      res.send({ code: 200, msg: 'ok', data: results })
    }
  )
})

//35.删除用户
serve.delete('/del_user/:uid', (req, res, next) => {
  let uid = req.params.uid
  let sql = 'delete from byd_user where uid=?'
  pool.query(sql, [uid], (error, results) => {
    if (error) {
      next(error)
      return
    }
    if (results.affectedRows == 0) {
      res.send({ message: 'fail to delete ', code: 201 })
    } else {
      res.send({ message: 'successfully delete', code: 200 })
    }
  })
})

//36.获取点赞数
serve.get('/getsupport', (req, res, next) => {
  // console.log(req.query)
  let mid=req.query.mid
  // sql语句
  let sql =
    'select scount from byd_fans_message where mid=?'
  pool.query(sql, [mid], (error, results) => {
    if (error) {
      next(error)
      return
    }
    // console.log(results)
    if (results.length == 0) {
      res.send({ message: 'inquire failed', code: 201 })
    } else {
      res.send({ message: 'inquire success', code: 200, data: results })
    }
  })
})

//指定服务器对象监听的端口号
serve.listen(8888, function () {
  console.log('WEB服务器启动成功')
})
